package cn.edu.sgu.www.mhxysy.service.potency.impl;

import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.potency.PotencyFruitPatternEffectDTO;
import cn.edu.sgu.www.mhxysy.entity.potency.PotencyFruitPatternEffect;
import cn.edu.sgu.www.mhxysy.exception.GlobalException;
import cn.edu.sgu.www.mhxysy.mapper.potency.PotencyFruitPatternEffectMapper;
import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.pager.potency.PotencyPatternEffectPager;
import cn.edu.sgu.www.mhxysy.restful.ResponseCode;
import cn.edu.sgu.www.mhxysy.service.potency.PotencyFruitPatternEffectService;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Service
public class PotencyFruitPatternEffectServiceImpl implements PotencyFruitPatternEffectService {

    private final PotencyFruitPatternEffectMapper potencyFruitPatternEffectMapper;

    @Autowired
    public PotencyFruitPatternEffectServiceImpl(PotencyFruitPatternEffectMapper potencyFruitPatternEffectMapper) {
        this.potencyFruitPatternEffectMapper = potencyFruitPatternEffectMapper;
    }

    @Override
    public void insert(PotencyFruitPatternEffectDTO potencyFruitPatternEffectDTO) {
        PotencyFruitPatternEffect potencyFruitPatternEffect = toEntity(potencyFruitPatternEffectDTO);

        potencyFruitPatternEffect.setId(null);

        potencyFruitPatternEffectMapper.insert(potencyFruitPatternEffect);
    }

    @Override
    public void deleteById(Integer id) {
        potencyFruitPatternEffectMapper.deleteById(id);
    }

    @Override
    public void updateById(PotencyFruitPatternEffectDTO potencyFruitPatternEffectDTO) {
        ObjectUtils.requireNonNull(potencyFruitPatternEffectDTO.getId(), CustomMessages.ID_NOT_NULL);

        PotencyFruitPatternEffect potencyFruitPatternEffect = toEntity(potencyFruitPatternEffectDTO);

        potencyFruitPatternEffectMapper.updateById(potencyFruitPatternEffect);
    }

    @Override
    public Page<PotencyFruitPatternEffect> selectByPage(PotencyPatternEffectPager pager) {
        QueryWrapper<PotencyFruitPatternEffect> queryWrapper = new QueryWrapper<>();
        Page<PotencyFruitPatternEffect> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getPotencyId() != null,
                "potency_id", pager.getPotencyId()
        );

        return potencyFruitPatternEffectMapper.selectPage(page, queryWrapper);
    }

    private PotencyFruitPatternEffect toEntity(PotencyFruitPatternEffectDTO potencyFruitPatternEffectDTO) {
        Integer id = potencyFruitPatternEffectDTO.getId();
        Integer patternId = potencyFruitPatternEffectDTO.getPatternId();
        Integer propertyId = potencyFruitPatternEffectDTO.getPropertyId();

        // 验证重复
        QueryWrapper<PotencyFruitPatternEffect> queryWrapper = new QueryWrapper<>();

        queryWrapper.ne(id != null, "id", id);

        queryWrapper.eq("pattern_id", patternId);
        queryWrapper.eq("property_id", propertyId);

        Long count = potencyFruitPatternEffectMapper.selectCount(queryWrapper);

        if (count > 0) {
            throw new GlobalException(ResponseCode.CONFLICT, "保存失败，数据重复！");
        }

        // 封装并返回实体类
        PotencyFruitPatternEffect potencyFruitPatternEffect = new PotencyFruitPatternEffect();

        potencyFruitPatternEffect.setId(id);
        potencyFruitPatternEffect.setPatternId(patternId);
        potencyFruitPatternEffect.setPropertyId(propertyId);
        potencyFruitPatternEffect.setPropertyValue(potencyFruitPatternEffectDTO.getPropertyValue());

        return potencyFruitPatternEffect;
    }

}